Title: SQL W3S (By-BKK) / 110 Primary Key
Tags: Obsidian_to_Anki

Q: What does the PRIMARY KEY constraint do in SQL?
A: It is used to uniquely identify each record in a table

Q: True or False: PRIMARY KEY columns must have unique values and cannot be NULL
A: True

Q: Can a table in SQL have more than one PRIMARY KEY?
A: No, only one PRIMARY KEY is allowed per table

Q: How many columns can a PRIMARY KEY have?
A: It can be a single column or a combination of columns

Q: Example SQL: How do you define a PRIMARY KEY on the ID column when creating a table in MySQL?
A: PRIMARY KEY (ID)

Q: How do you define a PRIMARY KEY on the ID and LastName columns at table creation?
A: PRIMARY KEY (ID, LastName)

Q: Reverse card: PRIMARY KEY (ID)
A: What SQL statement defines the ID column as the PRIMARY KEY?

Q: How do you add a PRIMARY KEY to an existing table in SQL?
A: Use ALTER TABLE Persons ADD PRIMARY KEY (ID);

Q: Reverse card: ALTER TABLE Persons ADD PRIMARY KEY (ID);
A: What SQL statement adds a PRIMARY KEY constraint on the ID column of an existing table?

Q: How do you add a PRIMARY KEY on multiple columns after table creation?
A: Use ALTER TABLE Persons ADD CONSTRAINT PK_Person PRIMARY KEY (ID, LastName);

Q: Reverse card: ALTER TABLE Persons ADD CONSTRAINT PK_Person PRIMARY KEY (ID, LastName);
A: How can you add a composite PRIMARY KEY to an existing table?

Q: When adding a PRIMARY KEY using ALTER TABLE, what must the column(s) have?
A: They must be declared with NOT NULL

Q: How do you remove (drop) a PRIMARY KEY in MySQL?
A: ALTER TABLE Persons DROP PRIMARY KEY;

Q: Reverse card: ALTER TABLE Persons DROP PRIMARY KEY;
A: How do you delete a PRIMARY KEY constraint from a table in MySQL?

Q: How do you remove a PRIMARY KEY constraint in SQL Server, Oracle, or MS Access?
A: ALTER TABLE Persons DROP CONSTRAINT PK_Person

Q: Reverse card: ALTER TABLE Persons DROP CONSTRAINT PK_Person
A: What SQL statement removes a PRIMARY KEY constraint in SQL Server or Oracle?